/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 17/03/2012, 09:47:59 PM
 */

package frontera;

import control.CRUDDatos;
import control.TrazarGraficas;
import entidad.Medicion;
import entidad.Sistema;
import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author user
 */
public class Principal extends javax.swing.JFrame {
    static Integer contador=0;
    private Historico historico;
    private ValorActual vactual;
    private Predicciones predicciones;
    private Reporte reporte;
    private TrazarGraficas trazar;

    public static Sistema sistema = new Sistema();

    /** Creates new form Principal */
    public Principal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        presionTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        temperaturaTF = new javax.swing.JTextField();
        pluviosidadTF = new javax.swing.JTextField();
        guardarB = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        historicoB = new javax.swing.JButton();
        vactualB = new javax.swing.JButton();
        reporteB = new javax.swing.JButton();
        prediccionesB = new javax.swing.JButton();
        salirB = new javax.swing.JButton();
        historicoX = new javax.swing.JButton();
        vactualX = new javax.swing.JButton();
        reporteX = new javax.swing.JButton();
        prediccionesX = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Monitor de condiciones ambientales");

        jLabel2.setText("Temperatura (ºC)");

        jLabel1.setText("Ingresar nuevas mediciones:");

        jLabel4.setText("Pluviosidad (mm)");

        jLabel3.setText("Presión atmosférica (mmHg)");

        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(temperaturaTF, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(presionTF)
                            .addComponent(jLabel4)
                            .addComponent(pluviosidadTF))))
                .addContainerGap(51, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(136, Short.MAX_VALUE)
                .addComponent(guardarB)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(temperaturaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(presionTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pluviosidadTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addComponent(guardarB)
                .addContainerGap())
        );

        historicoB.setText("<html>Histórico de<br>resultados</html>");
        historicoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                historicoBActionPerformed(evt);
            }
        });

        vactualB.setText("Valor actual");
        vactualB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vactualBActionPerformed(evt);
            }
        });

        reporteB.setText("Reporte");
        reporteB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reporteBActionPerformed(evt);
            }
        });

        prediccionesB.setText("Predicciones");
        prediccionesB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prediccionesBActionPerformed(evt);
            }
        });

        salirB.setText("Salir");
        salirB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirBActionPerformed(evt);
            }
        });

        historicoX.setText("<html><b>X</b></html>");
        historicoX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                historicoXActionPerformed(evt);
            }
        });

        vactualX.setText("<html><b>X</b></html>");
        vactualX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vactualXActionPerformed(evt);
            }
        });

        reporteX.setText("<html><b>X</b></html>");
        reporteX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reporteXActionPerformed(evt);
            }
        });

        prediccionesX.setText("<html><b>X</b></html>");
        prediccionesX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prediccionesXActionPerformed(evt);
            }
        });

        jLabel5.setText("Visualizar:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(historicoB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(vactualB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(reporteB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(prediccionesB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(historicoX)
                                    .addComponent(vactualX)
                                    .addComponent(reporteX)
                                    .addComponent(prediccionesX)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(salirB, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(historicoX, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(historicoB, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vactualB)
                    .addComponent(vactualX))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reporteB)
                    .addComponent(reporteX))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prediccionesB)
                    .addComponent(prediccionesX))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(salirB)
                .addContainerGap())
        );

        jLabel6.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jLabel6.setText("Monitor de condiciones ambientales");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(56, 56, 56))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salirBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirBActionPerformed
        this.dispose();
    }//GEN-LAST:event_salirBActionPerformed

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed

        //Verifica que no se encuentren espacios vacíos en ningún campo
        if (pluviosidadTF.getText().trim().length() < 1 || temperaturaTF.getText().trim().length() < 1 || presionTF.getText().trim().length() < 1) {
            JOptionPane.showMessageDialog(null, "Hay campos sin llenar.\nPor favor, inténtelo nuevamente.");
            return;
        }

        //Verifica que los datos ingresados en cada campo sean numéricos
        if (!pluviosidadTF.getText().matches("[0-9]*") || !temperaturaTF.getText().matches("[0-9]*") || !presionTF.getText().matches("[0-9]*")) {
            JOptionPane.showMessageDialog(null, "Por favor ingrese datos válidos (numéricos) en todos los campos.");
            return;
        }

        //Pasar a tipo double
        Medicion medicion = new Medicion (Double.parseDouble(temperaturaTF.getText()), Double.parseDouble(pluviosidadTF.getText()), Double.parseDouble(presionTF.getText()));

        CRUDDatos crud = new CRUDDatos();
        crud.guardarDatos(medicion);
        
    }//GEN-LAST:event_guardarBActionPerformed

    private void historicoBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_historicoBActionPerformed

        trazar = new TrazarGraficas(sistema.getMediciones());
        try {
            trazar.crearGrafica(TrazarGraficas.tipo.temperatura, "Temperatura vs. tiempo","intento","C°","temperatura-historico"+contador.toString()+".png");
            trazar.crearGrafica(TrazarGraficas.tipo.presion,"Presion vs tiempo","intento","mmHg","presion-historico"+contador.toString()+".png");
            trazar.crearGrafica(TrazarGraficas.tipo.pluviosidad,"Pluviosidad vs tiempo","intento","ml agua/m^2","pluviosidad-historico"+contador.toString()+".png");
            
        } catch (Exception e) {e.printStackTrace();}    
        
        historico = new Historico(contador);
        historico.setVisible(true);
        
        contador++;
        
        
//        historico.setLocationRelativeTo(this);
    }//GEN-LAST:event_historicoBActionPerformed

    private void historicoXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_historicoXActionPerformed
        if (historico.isVisible())
            historico.dispose();
    }//GEN-LAST:event_historicoXActionPerformed

    private void vactualBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vactualBActionPerformed
        vactual = new ValorActual();
        vactual.setVisible(true);
    }//GEN-LAST:event_vactualBActionPerformed

    private void vactualXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vactualXActionPerformed
        if (vactual.isVisible())
            vactual.dispose();
    }//GEN-LAST:event_vactualXActionPerformed

    private void reporteBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reporteBActionPerformed
        reporte = new Reporte();
        reporte.setVisible(true);
    }//GEN-LAST:event_reporteBActionPerformed

    private void reporteXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reporteXActionPerformed
        if (reporte.isVisible())
            reporte.dispose();
    }//GEN-LAST:event_reporteXActionPerformed

    private void prediccionesBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prediccionesBActionPerformed
        predicciones = new Predicciones();
        predicciones.setVisible(true);
    }//GEN-LAST:event_prediccionesBActionPerformed

    private void prediccionesXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prediccionesXActionPerformed
        if (predicciones.isVisible())
            predicciones.dispose();
    }//GEN-LAST:event_prediccionesXActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton guardarB;
    private javax.swing.JButton historicoB;
    private javax.swing.JButton historicoX;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField pluviosidadTF;
    private javax.swing.JButton prediccionesB;
    private javax.swing.JButton prediccionesX;
    private javax.swing.JTextField presionTF;
    private javax.swing.JButton reporteB;
    private javax.swing.JButton reporteX;
    private javax.swing.JButton salirB;
    private javax.swing.JTextField temperaturaTF;
    private javax.swing.JButton vactualB;
    private javax.swing.JButton vactualX;
    // End of variables declaration//GEN-END:variables

}
